<?php

error_reporting(0);
session_start();

require 'connect.php';

// Username validation
$sql1 = "SELECT * FROM $user_table WHERE username='" . $_POST["user"] . "'";
$result1 = mysql_query($sql1);
$count1 = mysql_num_rows($result1);

if (strlen($_POST["user"]) >= 5) {
    if ($count1 != 1) {
        // OK
        $vuser = 1;
    } else {
        // Not available
        $vuser = 2;
    }
} else {
    // Less than 5
    $vuser = 3;
}

// Password validation
if (strlen($_POST["pass"]) >= 8) {
    if ($_POST["user"] != $_POST["pass"]) {
        // OK
        $vpass = 1;
    } else {
        // Username cannot be the same as password
        $vpass = 2;
    }
} else {
    // Less than 8
    $vpass = 3;
}

// Confirm password validation
($_POST["pass"] == $_POST["cpass"]) ? $vcpass = 1 : $vcpass = 2;


if ($vuser != 1 || $vpass != 1 || $vcpass != 1) {
    header("location: index.php?vuser=$vuser&vpass=$vpass&vcpass=$vcpass");
} else {
    // Rest of the validation
    $pass = md5($_POST["pass"]);
    
    $sql = "INSERT INTO $user_table (username, password)
    VALUES
    ('" . $_POST["user"] . "','$pass')";

    $bool = mysql_query($sql);
    mysql_close();

    if (!$bool) {
        header("location: index.php");
    } else {
        mkdir("Repository/" . $_POST["user"]);
        $myusername = $_POST["user"];
        $mypassword = $_POST["pass"];
        $_SESSION["myusername"] = $myusername;
        header("location: fourbox.php?folder=Repository/$myusername");
    }
}
?>
